/*使用AJAX对用户名进行验证*/
var request;
var result = "";

function validate() {
	var idField = document.getElementById("username");
	var url = "validate?username=" + escape(idField.value);
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	request.open("GET", url, true);
	request.onreadystatechange = callback;
	request.send(null);
}

function callback() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var msg = request.responseXML.getElementsByTagName("msg")[0];
			if(msg.childNodes[0].nodeValue=="可以使用") {
				document.getElementById("usernameMsg").innerHTML = "<font color='green' size='2'>"+msg.childNodes[0].nodeValue+"</font>";
			}
			else {
				document.getElementById("usernameMsg").innerHTML = "<font color='red' size='2'>"+msg.childNodes[0].nodeValue+"</font>";
			}
		}
	}
}
function callback2() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var msg = request.responseXML.getElementsByTagName("msg")[0];
			if(msg.childNodes[0].nodeValue=="验证码错误") {
				result = msg.childNodes[0].nodeValue;
			}
		}
	}
}
/**/
function checkPassword(cs) {
	if(cs == null || cs == "") {
		document.getElementById("passwordMsg").innerHTML="<font color='red' size='2'>密码不能为空</font>";
		return false;
	}else if(cs.length < 6 || cs.length > 20) {
		document.getElementById("passwordMsg").innerHTML="<font color='red' size='2'>密码长度为6至20位</font>";
		return false;
	}
	document.getElementById("passwordMsg").innerHTML="<font color='green'>√</font>";
	return true;
}

function checkPasswordConfirm(cs) {
	if(cs != document.getElementById("password").value) {
		document.getElementById("passwordConfirmMsg").innerHTML="<font color='red' size='2'>密码不一致</font>";
		return false;
	}else if(cs =="") {
		document.getElementById("passwordConfirmMsg").innerHTML="<font color='red' size='2'>密码确认不为空</font>";
		return false;
	}
	document.getElementById("passwordConfirmMsg").innerHTML="<font color='green'>√</font>";
	return true;
}

function checkQuesstion(cs) {
	if(cs.length < 4 || cs.length > 30) {
		document.getElementById("questionMsg").innerHTML="<font color='red' size='2'>请输入4至30个字符的自定义问题</font>";
		return false;
	}
	document.getElementById("questionMsg").innerHTML="<font color='green'>√</font>";
	return true;
}

function checkAnswer(cs) {
	if(cs.length < 4 || cs.length > 30) {
		document.getElementById("answerMsg").innerHTML="<font color='red' size='2'>请输入4至30个字符的问题答案</font>";
		return false;
	}
	document.getElementById("answerMsg").innerHTML="<font color='green'>√</font>";
	return true;
}
function checkValidateCode(v) {
	if(v=="") {
		document.getElementById("validateCodeMsg").innerHTML="<font color='red' size='2'>请输入验证码</font>";
		return false;
	}else {
		document.getElementById("validateCodeMsg").innerHTML="";
	}
	return true;
}

function checkAll() {	
	var usernameMsg = document.getElementById("usernameMsg");
	var password = document.getElementById("password").value;
	var passwordConfirm = document.getElementById("passwordConfirm").value;
	var myquestion = document.getElementById("myquestion");
	var mqi = document.getElementById("mqi").value;
	var answer = document.getElementById("answer").value;
	var validateCode = document.getElementById("validateCode").value;
	if(usernameMsg.innerHTML=="") {
		usernameMsg.innerHTML = "<font color='red' size='2'>用户名不为空</font>";
		return false;
	}
	if(usernameMsg.childNodes[0].color=='red') return false;
	if(password=="") {
		document.getElementById("passwordMsg").innerHTML="<font color='red' size='2'>密码不能为空</font>";
		return false;
	}else if(password.length < 6 || password.length > 20) {
		document.getElementById("passwordMsg").innerHTML="<font color='red' size='2'>密码长度为6至20位</font>";
		return false;
	}
	if(passwordConfirm != password) {
		document.getElementById("passwordConfirmMsg").innerHTML="<font color='red' size='2'>密码不一致</font>";
		return false;
	}else if(passwordConfirm =="") {
		document.getElementById("passwordConfirmMsg").innerHTML="<font color='red' size='2'>密码确认不为空</font>";
		return false;
	}
	if(myquestion.style.display=="block") {
		if(mqi.length < 4 || mqi.length > 30) {
			document.getElementById("questionMsg").innerHTML="<font color='red' size='2'>请输入4至30个字符的自定义问题</font>";
			return false;
		}
	}
	if(answer.length < 4 || answer.length > 30) {
		document.getElementById("answerMsg").innerHTML="<font color='red' size='2'>请输入4至30个字符的问题答案</font>";
		return false;
	}
	if(validateCode=="") {
		document.getElementById("validateCodeMsg").innerHTML="<font color='red' size='2'>请输入验证码</font>";
		return false;
	}else {
		var url = "validate!checkVC?validateCode=" + escape(validateCode);
		if (window.XMLHttpRequest) {
			request = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		request.open("GET", url, false);
		request.onreadystatechange = callback2;
		request.send(null);
		if(result!="") {
			document.getElementById("validateCodeMsg").innerHTML = "<font color='red' size='2'>验证码错误</font>";
			validateCode = "";
			var timenow = new Date().getTime(); 
			document.getElementById("img").src = "randomNum?d"+timenow;
			result = "";
			return false;
		}
	}
	return true;
}